package com.sdx.console.contract.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.BaseEntity;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.sdx.console.common.entity.BaseProvince;
import com.sdx.console.common.service.BaseDataService;
import com.sdx.console.contract.dao.ContractMemberDao;
import com.sdx.console.contract.entity.*;
import com.sdx.console.contract.service.ContractInfoService;
import com.sdx.console.contract.service.CreateContractService;
import com.sdx.console.customer.entity.CustomerInfo;
import com.sdx.console.customer.service.CustomerInfoService;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 创建订单接口
 */


@RequestMapping("/contract/createcontract")
@Controller
public class CreateContractController extends AuthorizedBaesController {
    private static final String MODEL="/contract/contractInfo";//指定页面路径
    private static final String DBName = "coredb";//数据库名称
    private static final String SQL="contract.serviceProcess";//md文件所在目录
    @Resource
    private ContractInfoService contractInfoService;
    @Resource
    private CustomerInfoService customerInfoService;

    @Resource
    private ContractMemberDao contractMemberDao;

    @Resource
    private SQLManager sqlManager;

    @Resource
    private BaseDataService baseDataService;

    @Resource
    private CreateContractService createContractService;

    /**
     * 初始化页面需要的列表数据，
     *
     */
    private void setPageParam(ModelAndView view, HttpServletRequest request) {
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setIsTeacher(1);
        List<CustomerInfo> customerInfoList =  customerInfoService.template(customerInfo);
        view.addObject("customerInfoList",customerInfoList);
        view.addObject("customerList", JSON.toJSONString(customerInfoList));
    }

    //====页面
    @GetMapping("/create.do")
    @Function("contract.contractInfo.create")
    public ModelAndView createPage(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/create.html");
        setPageParam(view,request);
        return view;

    }

    //服务详情页面 的 列表
    @GetMapping("/serviceList.do")
    @Function("contract.contractInfo.serviceList")
    public ModelAndView serviceDetails(HttpServletRequest request,String contractId) {
        ModelAndView view = new ModelAndView(MODEL+"/serviceList.html");
        setPageParam(view,request);

        Map<String,String> param = new HashMap<>();
        param.put("contractId",contractId);
        List<BaseEntity> list = sqlManager.select(SQL+".getServiceTypeList",BaseEntity.class,param);
        view.addObject("ServicesList", createContractService.listEntityToJson(list));
        return view;
    }

    //跳转到修改列表里
    @GetMapping("/editServiceDetails.do")
    public ModelAndView editServiceDetails(HttpServletRequest request,String contractId,String tpyeId){
        ModelAndView view = new ModelAndView(MODEL+"/serviceDetails.html");
        setPageParam(view,request);
        Map<String,String> param = new HashMap<>();
        param.put("contractId",contractId);
        param.put("typeId",tpyeId);
        List<BaseEntity> list = sqlManager.select(SQL+".getProcessContent",BaseEntity.class,param);
        System.out.println(list);
        view.addObject("processList", createContractService.listEntityToJson(list));

        return  view;
    }
    //处理流程时弹出的框
    @GetMapping("/openifream.do")
    public ModelAndView openifream(HttpServletRequest request){
        ModelAndView view = new ModelAndView(MODEL+"/openifream.html");
        setPageParam(view,request);
        return view;
    }


    //接收文件
    @ResponseBody
    @PostMapping("/upload.json")
    public JSONObject savaFile(@RequestParam(name="file",required = false)MultipartFile file,HttpServletRequest request){
        System.out.println(file+" "+request);
        JSONObject obj = createContractService.upload(file,request);
        return  obj;
    }


    @ResponseBody
    @PostMapping("/updateNodeSchedule.json")
    public String updateNodeSchedule(HttpServletRequest request , @RequestBody  JSONObject par){
        String updateSql="UPDATE `contract_service_record` \n" +
                " SET node_schedule = "+par.getString("newNodeSchedule")+" \n" +
                " WHERE " +
                "     contract_id="+par.getString("acontractid")+"  " +
                " AND type_id="+par.getString("cserviceid")+"  "+
                " AND node_id = "+par.getString("cnodeid")+" " ;
        int updatenum = sqlManager.executeUpdate(new SQLReady(updateSql));
        return String.valueOf(updatenum);
    }






    //=====接口
    //获取市
    @PostMapping("/getProv")
    @ResponseBody
    public String getProv(){
        List<BaseProvince> listEntity = sqlManager.execute(new SQLReady
                ("SELECT prov_id,prov_name FROM coredb.base_province WHERE prov_status = 1 ORDER BY prov_id "),BaseProvince.class);
        return JSON.toJSONString(listEntity);
    }

    //获取市
    @PostMapping("/getCity")
    @ResponseBody
    public String getCity(@RequestBody JSONObject pro){
        List<BaseCity> listEntity = sqlManager.execute(new SQLReady
                        ("SELECT * FROM coredb.base_city WHERE prov_id=? AND  city_STATUS=1   ",pro.getString("province")),
                BaseCity.class);
        JSON.toJSONString(listEntity);
        return JSON.toJSONString(listEntity);
    }

    //获取区
    @PostMapping("/getBorough")
    @ResponseBody
    public String getBorough(@RequestBody JSONObject cityObject){
        System.out.println(cityObject);

        List<BaseBorough> listEntity = sqlManager.execute(new SQLReady("SELECT * FROM coredb.base_borough WHERE city_id=? AND borough_STATUS=1",cityObject.getString("cityid")), BaseBorough.class);
        System.out.println("ssss: "+JSON.toJSONString(listEntity));
        return JSON.toJSONString(listEntity);
    }
    //获取学校
    @PostMapping("/getSchool")
    @ResponseBody
    public String getSchool(@RequestBody JSONObject schoolObject){
        System.out.println(schoolObject);

        List<BaseSchool> listEntity = sqlManager.execute(new SQLReady("SELECT * FROM coredb.`base_school` WHERE borough_id=? AND school_status=1",schoolObject.getString("boroughid")), BaseSchool.class);
        return JSON.toJSONString(listEntity);
    }



}
